package com.niiwoo.civet.trade.service.dubbo.intelligenceInvest;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.NewbieProjectConfig;
import com.niiwoo.civet.trade.dao.entity.ProjectPackage;
import com.niiwoo.civet.trade.dao.mapper.NewbieProjectConfigMapperExt;
import com.niiwoo.civet.trade.dto.request.IntelligenceInvestDubboRequest;
import com.niiwoo.civet.trade.dto.request.intelligenceInvest.*;
import com.niiwoo.civet.trade.dto.response.IntelligenceInvestOrderResponseDTO;
import com.niiwoo.civet.trade.dto.response.InvestingProjectDTO;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.*;
import com.niiwoo.civet.trade.enums.Intelligence.IntelligenceSwitchSourceEnum;
import com.niiwoo.civet.trade.enums.InvestOrderCloseReasonEnum;
import com.niiwoo.civet.trade.enums.PackageTypeEnum;
import com.niiwoo.civet.trade.service.intelligenceInvest.IntelligenceInvestDubboService;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.IntelligenceInvestService;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.NoviceIntelligenceInvestService;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.ProjectPackageLocalService;
import com.niiwoo.tripod.provider.dto.response.PageResponseDTO;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service(version = "1.0.0")
public class IntelligenceInvestDubboServiceImpl implements IntelligenceInvestDubboService{

    @Autowired
    private IntelligenceInvestService intelligenceInvestService;
    @Autowired
    private NoviceIntelligenceInvestService noviceIntelligenceInvestService;

    @Autowired
    private NewbieProjectConfigMapperExt newbieProjectConfigMapperExt;

    @Autowired
    private ProjectPackageLocalService projectPackageLocalService;

    @Override
    public IntelligenceInvestSwitchResponseDTO intelligenceInvestSwitch(Byte sourceCode) {
        return intelligenceInvestService.intelligenceInvestSwitch(sourceCode);
    }

    @Override
    public void designatedPackage(DesignatedPackageRequestDTO requestDTO) {
        projectPackageLocalService.designatedPackage(requestDTO);
    }

    @Override
    public void cancelPublish(CancelPublishRequestDTO requestDTO) {
        projectPackageLocalService.cancelPublish(requestDTO);
    }

    @Override
    public void firstPublish(FirstPublishRequestDTO requestDTO) {
        projectPackageLocalService.firstPublish(requestDTO);
    }

    @Override
    public void doPackageForJob() {
        projectPackageLocalService.doPackageForJob();
    }

    @Override
    public void addProjectForJob() {
        projectPackageLocalService.addProjectForJob();
    }

    @Override
    public void doPublishForJob() {
        projectPackageLocalService.doPublishForJob();
    }

    @Override
    public PageResponseDTO<IntelligenceInvestResponseDTO> getIntelligenceInvestList(IntelligenceInvestPageRequestDTO requestDTO) {
        Assert.notNull(requestDTO, "请求参数不能为空");
        IntelligenceInvestSwitchResponseDTO isOpen = intelligenceInvestService.intelligenceInvestSwitch(requestDTO.getSourceCode());
        if(isOpen.getIntelligenceInvestSwitch() == 0){
            throw new BizException("TRD10033");//智能投未开启
        }
        if(IntelligenceSwitchSourceEnum.NORMAL.getCode().equals(requestDTO.getSourceCode())) {
        	 if (requestDTO.getUserId() != null) {
                 //用户新手智能投相关信息
                 NoviceIntelligenceInvestInfoResponseDTO dto = noviceIntelligenceInvestService.getNoviceBasicInfo(requestDTO.getUserId());
                 //注册在Y天以内的切可投大于0，展示新手智能投* 1.展示，0 不展示
                 requestDTO.setUserIdentity((dto.getNoviceSurplusDays() > 0 &&
                         dto.getNoviceSurplusInvestMoney().compareTo(BigDecimal.ZERO) > 0) ? (byte) 1 : (byte) 0);
             } else {
                 requestDTO.setUserIdentity((byte) 1);
             }
        }
       
        return intelligenceInvestService.getIntelligenceInvestList(requestDTO);
    }

    
    @Override
    public IntelligenceInvestDetailResponseDTO getIntelligenceInvestDetail(IntelligenceInvestDetailRequestDTO requestDTO) {
    	IntelligenceInvestSwitchResponseDTO isOpen = intelligenceInvestService.intelligenceInvestSwitch(requestDTO.getSourceCode());
        if(isOpen.getIntelligenceInvestSwitch() == 0){
            throw new BizException("TRD10033");//智能投未开启
        }
        IntelligenceInvestDetailResponseDTO dto = intelligenceInvestService.getIntelligenceInvestDetail(requestDTO);
        String userId = requestDTO.getUserId();
        if(PackageTypeEnum.NOVICE_INTELLIGENCE.getType().equals(dto.getPackageType())){
            if (userId == null) {
                NewbieProjectConfig newbieProjectConfig = newbieProjectConfigMapperExt.selectNewbieProjectConfig(null);
                dto.setNoviceHint("新手智能投，新手可投，累计限额" + newbieProjectConfig.getInvestmentNorm() + "元");
            } else {
                NoviceIntelligenceInvestInfoResponseDTO noviceInvestInfoResponseDTO = noviceIntelligenceInvestService.getNoviceBasicInfo(userId);
                dto.setNoviceHint("新手智能投，新手可投，累计限额" + noviceInvestInfoResponseDTO.getInvestmentNorm() + "元");
                dto.setNoviceSurplusInvestMoney(noviceInvestInfoResponseDTO.getNoviceSurplusInvestMoney());
                dto.setRoleDefinitionDays(noviceInvestInfoResponseDTO.getRoleDefinitionDays());
                dto.setNoviceSurplusDays(noviceInvestInfoResponseDTO.getNoviceSurplusDays());
            }

        }
        return dto;
    }
    @Override
    public ProjectPackageResponseDTO getIntelligenceInvestInfo(Long packageId) {
        Assert.notNull(packageId,"包id不能为空");
        return intelligenceInvestService.getIntelligenceInvestInfo(packageId);
    }

    @Override
    public PageResponseDTO<InvestingProjectDTO> getIntelligenceInvestTotalProjectInfos(IntelligenceInvestCombinationRequestDTO dto) {
        Assert.notNull(dto, "请求参数不能为空");
        Assert.notNull(dto.getPackageId(),"包id不能为空");
        return intelligenceInvestService.getIntelligenceInvestTotalProjectInfos(dto);
    }

    @Override
    public NoviceIntelligenceInvestInfoResponseDTO getNoviceInvestInfos(String userId) {
        return noviceIntelligenceInvestService.getNoviceBasicInfo(userId);
    }
    @Override
    public IntelligenceInvestOrderResponseDTO intelligenceInvest(IntelligenceInvestDubboRequest request) {
    	 
    	return intelligenceInvestService.intelligenceInvest(request);
    }

    
    @Override
    public void flow() {

    	intelligenceInvestService.flow();
    }

    @Override
    public void investCancelSuccess(Long projectPackageInvestOrderId, Long investOrderId, InvestOrderCloseReasonEnum closeReasonEnum) {
    	 
    	intelligenceInvestService.investCancelSuccess(projectPackageInvestOrderId, investOrderId, closeReasonEnum);
    	
    }
    
    @Override
    public void triggerInvest() {
    	intelligenceInvestService.triggerInvest();
    }
    
    @Override
    public void triggerCancel() {
    	intelligenceInvestService.triggerCancel();
    }
    @Override
    public List<IntelligencePayInfoResponseDTO> getIntelligencePayInfo(Long projectPackageOrderId) {
    	return intelligenceInvestService.getIntelligencePayInfo(projectPackageOrderId);
    }
    
    @Override
    public void intelligenceInvestSuccess(Long packageInvestOrderId) {
    	 
    	intelligenceInvestService.intelligenceInvestSuccess(packageInvestOrderId);
    }
    
    
    @Override
    public void intelligenceInvestPreCancel(Long projectPackageOrderId) {
    	
    	intelligenceInvestService.intelligenceInvestPreCancel(projectPackageOrderId);
    }
    @Override
    public void intelligenceCancel(Long projectPackageOrderId) {
    	intelligenceInvestService.intelligenceCancel(projectPackageOrderId);
    }

    @Override
    public ProjectPackageResponseDTO getProjectPackageByProjectId(Long projectId){
        ProjectPackageResponseDTO projectPackageResponseDTO = new ProjectPackageResponseDTO();
        ProjectPackage projectPackage = intelligenceInvestService.getProjectPackageByProjectId(projectId);
        if(projectPackage == null){
            return null;
        }
        BeanUtils.copyProperties(projectPackage,projectPackageResponseDTO);
        return projectPackageResponseDTO;
    }
    
    
    @Override
    public void unfreeAmount() {
     
    	intelligenceInvestService.unfreeAmount();
    }
    
    @Override
    public SelectIntelligenceDeadlineResponseDTO selectIntelligenceDeadline(SelectIntelligenceDeadlineRequestDTO requestDto) {
    	 
    	return intelligenceInvestService.selectIntelligenceDeadline(requestDto);
    }

    @Override
    public IsSmartInvestSoldOutResponseDTO queryIntelligenceInvestCount(String userId) {
        Byte userIdentity;
        if (StringUtils.isNotBlank(userId)) {
            //用户新手智能投相关信息
            NoviceIntelligenceInvestInfoResponseDTO dto = noviceIntelligenceInvestService.getNoviceBasicInfo(userId);
            //展示新手智能投* 1.展示，0 不展示
            userIdentity = (dto.getNoviceSurplusDays() > 0 &&
                    dto.getNoviceSurplusInvestMoney().compareTo(BigDecimal.ZERO) > 0) ? (byte) 1 : (byte) 0;

        } else {
            userIdentity = (byte) 1;
        }
        return intelligenceInvestService.queryIntelligenceInvestCount(userIdentity,IntelligenceSwitchSourceEnum.NORMAL.getCode());
    }
    
}
